const localpath = "http://192.168.0.102:666/";
//http://tailor.weexoa.com/index.js
const rootPath = "http://www.midianvip.cn:8080";
var basePath = rootPath;
var pref = weex.requireModule("storage");
var stream = weex.requireModule("stream");
var modal = weex.requireModule("modal");
const navigator = weex.requireModule("navigator");
var token = pref.getItem('token');
var asCore = {
    localpath: localpath,
    rootPath: basePath,
    setContext:function(value){
        value = value ? JSON.stringify(value) : '';
        pref.setItem('context', value);
    },
    getContext:function(callback){
        pref.getItem('context', event => {
            var value = event.data;
            try {
                value = JSON.parse(value);
            } catch (error) {
                value = {};
            }
            callback && callback(value);
        });
    },
    setToken: function (value) {
        pref.setItem('token', value);
    },
    getToken: function (callback) {
        pref.getItem('token', event => {
            var value = event.data;
            event.result != "success" && (value = "");
            callback && callback(value);
        });
    },
    /* 与web端的ajax用法一致 */
    post: function (action, param, callBack) {
        let options = {
            url: basePath + action,
            method: "POST",
            headers:{},
            type: "json",
            body: JSON.stringify(param),
        };
        pref.getItem('token', e => {
            options.headers["Content-Type"] = 'application/json';
            options.headers["Authorization"] = "bearer " + (e.data?e.data:"");
            stream.fetch(options,(ret) => {
                //this.alert(JSON.stringify(ret));
                if (!ret.ok) {
                    if (ret.status == 550) {
                        callBack && callBack(null,"timeout", false);
                    } else {
                        callBack && callBack(null,ret.data,false);
                    }
                } else {
                    var data = ret.data;
                    try {
                        if(data.code == "SUCCESS"){
                            callBack && callBack(data['data'], "成功",true);
                        }else{
                            callBack && callBack(null, data['msg'],false);
                        }
                    } catch (e) {
                        callBack && callBack(null, "网络错误",false);
                    }
                }
            });
        });
    },
    logout: function (callback) {
        this.post("/member/logout", {}, (data,msg,flag)=> {
            pref.removeItem("token");
            if (callback) callback(data);
        });
    },
    toParams: function (obj) {
        var param = ""
        for (const name in obj) {
            if (typeof obj[name] != 'function') {
                param += "&" + name + "=" + encodeURI(obj[name])
            }
        }
        return param.substring(1)
    },
    toast: function (msg) {
        modal.toast({
            message: msg
        });
    },
    alert: function (msg) {
        modal.alert({
            message: msg
        });
    }

};
export default asCore;